<?php
///
///  composer 包
///  "maatwebsite/excel": "^3.1",
///
///

namespace App\Excel;


use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithDrawings;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnWidths;

class Export implements FromArray,ShouldAutoSize,WithDrawings,WithColumnWidths
{

    public $data;
    public $logo;

    public function __construct($data,$logo)
    {
        $this->data = $data;
        $this->logo = $logo;
    }

    public function array(): array
    {
        // TODO: Implement collection() method.
        return $this->data;
    }

    public function columnWidths(): array
    {
        return [
            'H' => 50,
            'I' => 50,
            'J' => 50,
            'K' => 50,
            'L' => 50,
        ];
    }

    public function drawings()
    {
        $result = [];
        foreach ($this->logo as $k => $v){
            try {
                $k +=2;
                foreach ($v as $k1 => $v1){
                    $k2 = $k1;
                    $k1 += $k;
                    ${'drawing'.$k1} = new Drawing();
                    ${'drawing'.$k1}->setName('Logo');
                    ${'drawing'.$k1}->setDescription('This is my logo');
                    ${'drawing'.$k1}->setPath(base_path('public'.$v1));
                    ${'drawing'.$k1}->setHeight(90);
                    if ($k2 == 0){
                        ${'drawing'.$k1}->setCoordinates('H'.$k1);
                    }elseif ($k2 == 1){
                        ${'drawing'.$k1}->setCoordinates('I'.($k1-1));
                    }elseif ($k2 == 2){
                        ${'drawing'.$k1}->setCoordinates('J'.($k1-2));
                    }elseif ($k2 == 3){
                        ${'drawing'.$k1}->setCoordinates('K'.($k1-3));
                    }elseif ($k2 == 4){
                        ${'drawing'.$k1}->setCoordinates('L'.($k1-4));
                    }

                    $result[] = ${'drawing'.$k1};
                }

            }catch (\Exception $e){

            }
        }
        return $result;
    }

}


/***
 * 签到奖励管理类
 */

namespace App\Http\Controllers\Admin;


use Illuminate\Http\Request;
use App\Excel\Export;
use Maatwebsite\Excel\Facades\Excel;
use App\Models\Feedback;
use Illuminate\Support\Facades\DB;


class FeedbackAdminController extends BaseAdminController
{
    public $where = [];

    /**
     * 用户反馈导出
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function exportExcelFeedBackAdmin(Request $request)
    {
        $where = [];
        $starttime = $request->get('start_at');
        $endtime = $request->get('end_at');
        if (!empty($starttime)) {
            $starttime = strtotime($starttime);
            $where[] = ['created_at', '>', $starttime];
        }
        if (!empty($endtime)) {
            $endtime = strtotime($endtime);
            $where[] = ['created_at', '<', $endtime];
        }

        $header = [
            'id', '用户id', '牌局id', '反馈内容', '手机号', '微信号', '反馈时间'
        ];
        $list = Feedback::where($where)->get()->toArray();
        $temp_img = [];
        foreach ($list as $key => &$val) {
            if (!empty($val['imgs'])) {
                $temp_img[] = unserialize($val['imgs']);
            }
        }
        $list = Feedback::where($where)->get(['id', 'uid', 'game_id', 'content', 'phone', 'wechat', 'created_at'])->toArray();
        foreach ($list as $key => &$val) {
            $val['created_at'] = date('Y-m-d H:i:s');
        }
        array_unshift($list, $header);

        $filepath = 'feedback' . time() . '.xlsx';
        $export_feedback = Excel::store((new Export($list, $temp_img)), $filepath, 'feedback');
        if ($export_feedback) {
            return $this->apiSuccess('导出用户反馈成功', ['path' => env('APP_URL') . '/storage/feedback/' . $filepath]);
        } else {
            return $this->apiFail('导出用户反馈失败');
        }
    }
}

